﻿/*
创建人：zhangrj
日期：2023-03-28
描述：CPK分析页面js
*/
var page = {
    pageTotal: 0,
    pageSize: 10,
    pageNumber: 1,
    ajaxUrl: getRootPath() + "/Ashx/StepCheckPointResultSPCHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
    },
    //页面事件绑定
    bind: function () {
        //结果列表加载
        $("#gdCheckPointResult").datagrid({
            columns: [[
                 { field: 'CREATEDATE', title: '录入时间', width: 100,
                     formatter: function (value, row, index) {
                         if (!isNullOrWhiteSpaceStr(value)) {
                             return dayjs(value).format('YYYY-MM-DD');
                         }
                     }
                 },
                 { field: 'CONTAINERNAME', title: '批次', width: 180 },
                 { field: 'RESOURCEDESC', title: '操作设备', width: 150 },
                 { field: 'WORKNAME', title: '操作者', width: 80 },
                 { field: 'CURRDATA', title: '实测值', width: 120 },
                 { field: 'SPCSTATUS', title: '状态', width: 80,
                     formatter: function (value, row, index) {
                         if (value === 1) {
                             return "<span style='color:#FF0000'>异常</span>";
                         }
                         else {
                             return "<span style='color:#00CD00'>正常</span>";
                         }
                     }
                 },
                 { field: 'SPCRULE', title: '判异规则', width: 100 },
                 { field: 'BASEVALUE', title: '基本值', width: 80 },
                 { field: 'UPDATA', title: '上限值', width: 80 },
                 { field: 'DWDATA', title: '下限值', width: 80 }
                ]]
        });
        $('#btnSearch').on("click", function () {
            page.search();
        });
        $('#btnReset').on("click", function () {
            page.clearPage(true, true, true);
        });
    },
    //填充工序下拉框
    fillStepData: function (workflowID) {
        var formData = {
            action: "GetWorflowStepListData",
            workflowID: workflowID
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            //工序下拉
            $("#selWorkflowStep").combobox({
                valueField: 'WORKFLOWSTEPID',
                textField: 'WORKFLOWSTEPNAME',
                panelHeight: 200,
                data: res.DBTable, //下拉数据
                onSelect: function (record) {
                    page.fillStepCheckPoint(record.WORKFLOWSTEPID);
                }
            });
        });
    },
    //填充检测项下拉列表
    fillStepCheckPoint: function (workflowstepId) {
        var formData = {
            action: "GetStepCheckPointDataByStepId",
            workflowstepId: workflowstepId
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $('#selCheckPoint').combogrid({
                multiple: false, //设置多选
                panelWidth: 300,
                panelHeight: 200,
                idField: 'STEPCHECKPOINTID',
                textField: 'NAME',
                checkOnSelect: true,
                columns: [[
                         { field: 'ck', title: '', width: 50, checkbox: true },
                         { field: 'CHECKPOINTNOTEDIS', title: '检测项内容', width: 150 },
                         { field: 'KEYCHECKNAME', title: '关键过程检', width: 80 },
                         { field: 'CHECKTYPENAME', title: '关键过程类型', width: 80 }
                 ]],
                data: res.DBTable
            });
        });
    },
    //查询
    search: function () {
        var row = $('#selCheckPoint').combogrid('grid').datagrid('getChecked');
        if (row.length === 0) {
            displayMessage("请选择检测项", false);
            return;
        }
        var formData = {
            action: "GetSpcByCpkData",
            productID: $("#productContent").productCombo("getValue"),
            workflowID: $("#workflowContent").workflowCombo("getValue"),
            workflowStepID: $("#selWorkflowStep").combobox("getValue"),
            stepCheckPointID: row[0].STEPCHECKPOINTID,
            startTime: $("#txtStartTime").datebox("getValue"),
            endTime: $("#txtEndTime").datebox("getValue")
            //            sPCRule: $("#selSPCRule").combobox("getValue")
        };
        page.post(page.ajaxUrl, formData, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            var dtRes = res.DBDataSet.dtCheckPointResult;
            $("#gdCheckPointResult").datagrid("loadData", { rows: dtRes });
            $("#divCPK").html("CPK：" + dtRes[0].CPK);
            var datas = { xData: [], currData: [], baseData: [], upData: [], dwData: [] };
            for (var i = 0; i < dtRes.length; i++) {
                datas.xData.push(i + 1); //x轴数据
                datas.currData.push(dtRes[i].CURRDATA); //实测值
                datas.baseData.push(dtRes[i].BASEVALUE); //基本值
                datas.upData.push(dtRes[i].UPDATA); //上限值
                datas.dwData.push(dtRes[i].DWDATA); //下限值
            }
            datas.maxYdata = dtRes[0].MAXYDATA; //Y轴最大值
            datas.minYdata = dtRes[0].MINYDATA; //Y轴最小值
            controlSingleView.init(datas);
            
        });
    },
    //禁用页面按钮
    disablePageBtn: function () {
        //禁用主页面所有按钮
        $("#mainDiv").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('disable');
        });
    },
    //恢复页面按钮
    enablePageBtn: function () {
        //恢复主页面所有按钮
        $("#mainDiv").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('enable');
        });
    },
    //清除页面（查询区域，图形区域，是否清空消息）
    clearPage: function (query, list, msg) {
        if (query) {
            $("#productContent").productCombo("init"); //件号
            $("#workflowContent").workflowCombo("init"); //工艺
            $("#selWorkflowStep").combo("clear"); //工序
            $("#selWorkflowStep").combo("panel").empty();
            $("#selCheckPoint").combogrid("clear"); //检测项
            $("#selCheckPoint").combogrid('grid').datagrid('loadData', { rows: [] });
            $("#txtStartTime").datebox("setValue", "");
            $("#txtEndTime").datebox("setValue", "");
        }
        if (list) {
            $("#controlSingleView").html("");
            clearEasyuiDataGrid("gdCheckPointResult");
        }
        if (msg) {
            clearMessage();
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
//控制图，单值
var controlSingleView = {
    init: function (datas) {
        var dom = document.getElementById('controlSingleView');
        var myChart = echarts.init(dom);
        var option = {
            //            color: ['#00DDFF'],
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['上限值', '基本值', '实测值', '下限值']
            },
            grid: {
                top: '6%',
                right: '3%',
                left: '0%',
                bottom: '0%',
                containLabel: true
            },
            xAxis: [{
                type: 'category',
                boundaryGap: false,
                axisLabel: {
                    interval: 0 //x轴全部显示
                },
                data: datas.xData
            }],
            yAxis: [{
                type: 'value',
                max: datas.maxYdata,
                min: datas.minYdata
            }],
            series: [
             {
                 name: '上限值',
                 type: 'line',
                 data: datas.upData
             },
             {
                 name: '基本值',
                 type: 'line',
                 data: datas.baseData
             },
            {
                name: '实测值',
                type: 'line',
                data: datas.currData
            },
            {
                name: '下限值',
                type: 'line',
                data: datas.dwData
            }
            ]
        };
        myChart.setOption(option);
        window.addEventListener('resize', myChart.resize);
    }
};
//件号-工艺路线组件对象（不能页面加载后绑定，不然无法生效）
var productLinkWorkflow = {
    init: function () {
        //绑定件号选择组件
        $("#productContent").productCombo({
            search: function () { //查询事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
                page.clearPage();
            },
            cancel: function () { //件号取消点击事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                page.clearPage();
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
            },
            selected: function (val, text) {//件号下拉选择事件
                clearEasyuiDataGrid("gdStepCheckPoint");
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
                $("#workflowContent").workflowCombo("setCombo", { productName: text }); //联动工艺下拉
                page.clearPage();
            }
        });
        //绑定工艺选择组件
        $("#workflowContent").workflowCombo({
            cancel: function () {//工艺取消点击事件
                page.clearPage();
            },
            selected: function (val, text) {//工艺下拉选择事件
                page.fillStepData(val);
            }
        });
    }
};
productLinkWorkflow.init();
$(function () {
    page.init();
});